/*
*************************************************************************
本文件归属于西安协同时光软件有限公司，未经许可，不得擅自使用、拷贝和修改
*项目组:解决方案部项目开发框架
*版 本: 1.0   
*日 期：March 10, 2010   
*时 间：9:32:00 AM   
*作 者：Alex 
*************************************************************************   
*/  
package com.manager.commons.tag.el;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import org.apache.commons.lang.BooleanUtils;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

/**   
 * TODO 配置文件参数文件   *   
 * @author  Alex   
 * @since  jdk1.6   */
public class ExpressionEvaluatorUtil
{

    /**
     * page context.
     */
    private PageContext pageContext;

    /**
     * tag.
     */
    private Tag tag;


    public ExpressionEvaluatorUtil(Tag sourceTag, PageContext context)
    {
        this.tag = sourceTag;
        this.pageContext = context;
    }

    /**
     * 得到el表达式的值
     * @param attrName 
     * @param attrValue 
     * @return evaluate 
     * @param returnClass 
     */
    public Object eval(String attrName, String attrValue, Class returnClass) throws JspException
    {
        return attrValue != null ? ExpressionEvaluatorManager.evaluate(
            attrName,
            attrValue,
            returnClass,
            this.tag,
            this.pageContext) : null;
    }


    public String evalString(String attrName, String attrValue) throws JspException
    {
        return (String) eval(attrName, attrValue, String.class);
    }


    public boolean evalBoolean(String attrName, String attrValue) throws JspException
    {
        return BooleanUtils.toBoolean((Boolean) eval(attrName, attrValue, Boolean.class));
    }


    public long evalLong(String attrName, String attrValue) throws JspException
    {
        Long rtn = (Long) eval(attrName, attrValue, Long.class);
        if (rtn != null)
        {
            return rtn.longValue();
        }

        return -1L;
    }


    public int evalInt(String attrName, String attrValue) throws JspException
    {
        Integer rtn = (Integer) eval(attrName, attrValue, Integer.class);
        if (rtn != null)
        {
            return rtn.intValue();
        }

        return -1;
    }
}